/*
	Copyright (c) 2008, Grant Jones <grant@podtronics.com>.
	All rights reserved.

	Redistribution and use in source and binary forms, with or
	without modification, are permitted provided that the following
	conditions are met:

		Redistributions of source code must retain the above copyright
		notice, this list of conditions and the following disclaimer.
		
		Redistributions in binary form must reproduce the above copyright
		notice, this list of conditions and the following disclaimer in
		the documentation and/or other materials provided with the
		distribution.
		
		Neither the name of the gxjones.com nor the names of its
		contributors may be used to endorse or promote products derived
		from this software without specific prior written permission.
	
	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
	LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
	FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
	COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
	INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
	(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
	SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
	OF THE POSSIBILITY OF SUCH DAMAGE.
	
	http://www.opensource.org/licenses/bsd-license.php
*/

/*
 *  tagmt.h
 *  radixtree
 *
 *  Created by Grant Jones on 2/29/08.
 *
 */

/*
** Tag Transaction Management Routines
**
** TODO:
** - transaction level rollbacks are not yet implemented, a failed
**   transaction commit to a file may leave the file in an inconsistent
**   state, see tagmt_endTransaction where it writes to the file.
** - look into aio for improved performance?
*/

#ifndef TAGMT_H
#define TAGMT_H

#define MAX_CACHE_ITEM			65536	/* setting this value to zero effective disables transactions */


typedef struct TagManagerTC
{
	/* number of writes made to this cache item; at the end of a transaction
	** if this number is zero it is not written to the file
	*/
	int writeCount;	

	off_t offset;
	size_t nbyte;
	unsigned char *data[1];
} TagManagerTC;

typedef struct TagManagerTrans
{
	struct TagManagerTrans *link;

	int tid;
	size_t writeCount;	/* total number of writes, optimization for read-only operations */
	void *cache;		/* cache -> cmapStruct * created by cmap */
} TagManagerTrans;

void tagmt_init( TagManagerStruct *tags );

/* transaction management interface routines */

int tagmt_beginTransaction( TagManagerStruct *tags );
int tagmt_endTransaction( TagManagerStruct *tags, int tid );
int tagmt_abortTransaction( TagManagerStruct *tags, int tid );

size_t tagmt_pread( TagManagerStruct *tags, void *buf, size_t nbyte, off_t offset );
size_t tagmt_pwrite( TagManagerStruct *tags, void *buf, size_t nbyte, off_t offset );

/* journal protos */
int tagmt_journal_begin( char *journal_template );
int tagmt_journal_end( char *journal_template, int journal_fd );
int tagmt_journal_write( int journal_fd, void *old_buf, void *new_buf, size_t nbyte, off_t offset );
int tagmt_journal_rollback( int journal_fd, TagManagerStruct *tags );


#endif


